package com.canbot.u05.activity.attendance;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.bean.AttendaceExcelBean;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.ExcelDataLoader;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.CalendarView;
import com.canbot.u05.view.ChartTableView;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 考勤信息统计界面
 */
public class AttendanceShowInfoActivity extends Activity implements View.OnClickListener {

        private static final String TAG = "AttendanceShowInfoActivity";

        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        private SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");

        private ImageView imageView_back;

        private EditText editText;

        private Button btnSure;

        private ChartTableView chartTableView;

        private List<AttendaceExcelBean> dataList;

        private TextView tvYearAndMonth;

        private ImageButton imageButton,btnDateDelete;

        private CalendarView calendarView;

        private RelativeLayout rl;

        private String month;

        private Dialog dialogLoading;

        ExcelDataLoader m;

        private MyReceiver myReceiver;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_attendance_show_info);
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));

                month = getIntent().getStringExtra("month");

                if (MainApplication.getInstance().downLoadSuccess){
                        m=  new ExcelDataLoader(this);
                        m.execute(ConstUtils.PATH_SD + "/kq/" + month + ".xls");
                }
                myReceiver = new MyReceiver();
                IntentFilter intentFileter = new IntentFilter();
                intentFileter.addAction(ConstUtils.ACTION_KQ_DOWNLOAD_SUCCESS);
                LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver,intentFileter);


        }

        public void updateData(ArrayList<AttendaceExcelBean> attendaceExcelBeen) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                setViews();
                Logger.e(TAG,"查询完成= "+ attendaceExcelBeen.toString());
                this.dataList = attendaceExcelBeen;
                chartTableView.setDataList(dataList);

        }

        private void setViews() {

                imageView_back = (ImageView) findViewById(R.id.imageView_back);
                chartTableView = (ChartTableView) findViewById(R.id.chartTableView);
                editText = (EditText) findViewById(R.id.et_name);
                btnSure = (Button) findViewById(R.id.btn_sure);
                tvYearAndMonth = (TextView)findViewById(R.id.tv_date);
                rl = (RelativeLayout)findViewById(R.id.rl_select);
                imageButton = (ImageButton)findViewById(R.id.bt_dropdown);
                btnDateDelete = (ImageButton)findViewById(R.id.bt_delete_date);
                imageButton.setOnClickListener(this);
                btnDateDelete.setOnClickListener(this);

//                tvYearAndMonth.setText(month);
                calendarView = (CalendarView)findViewById(R.id.calendar);
                try {
                        calendarView.setMonth(sdf.parse(month + "-01"));
                }
                catch (ParseException e) {
                        e.printStackTrace();
                }

                imageView_back.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                calendarView.setOnItemClickListener(new CalendarView.OnItemClickListener() {
                        @Override
                        public void OnItemClick(Date date) {
                                rl.setVisibility(View.GONE);
                                String str=sdfDate.format(date);
                                tvYearAndMonth.setText(str);
                                btnDateDelete.setVisibility(View.VISIBLE);
                                btnDateDelete.setClickable(true);
                        }
                });
                editText.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                                if (TextUtils.isEmpty(s.toString().trim())) {
                                        initChartTable(dataList);
                                }
                        }
                });


        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:

                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;

                        case R.id.btn_sure:

                                ArrayList<AttendaceExcelBean> a = new ArrayList<>();
                                for (int i = 0; i < dataList.size(); i++) {
                                        if (TextUtils.isEmpty(editText.getText())){
                                                ToastTools.newInstance(AttendanceShowInfoActivity.this).showText(AttendanceShowInfoActivity.this.getString(R.string.name_cannot_empty));

                                                return;
                                        }
                                        //日期未选择时
                                        if (!TextUtils.isEmpty(tvYearAndMonth.getText().toString())){
                                                if (editText.getText().toString().trim().equals(dataList.get(i).getName()) && tvYearAndMonth.getText().toString().trim().equals(dataList.get(i).getDate())) {
                                                        a.add(dataList.get(i));
                                                }
                                        }else {
                                                if (editText.getText().toString().trim().equals(dataList.get(i).getName())) {
                                                        a.add(dataList.get(i));
                                                }
                                        }
                                }
                                if (a.size() == 0){
                                        ToastTools.newInstance(AttendanceShowInfoActivity.this).showText(AttendanceShowInfoActivity.this.getString(R.string.no_time_sheet));
                                        return;
                                }
                                initChartTable(a);
                                break;

                        case R.id.bt_dropdown:
                                rl.setVisibility(View.VISIBLE);
                                rl.bringToFront();
                                btnDateDelete.setClickable(false);
                                break;

                        case R.id.bt_delete_date:
                                //删除日期
                                tvYearAndMonth.setText("");
                                btnDateDelete.setVisibility(View.GONE);
                                break;
                }
        }

        private void initChartTable(List<AttendaceExcelBean> attendaceExcelBeen) {
                //将数据传入自定义控件里
                chartTableView.setDataList(attendaceExcelBeen);
        }

        private class MyReceiver extends BroadcastReceiver {

                @Override
                public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        switch (action){
                                case ConstUtils.ACTION_KQ_DOWNLOAD_SUCCESS:
                                        Logger.e(TAG,"下载完成");
                                        m= new ExcelDataLoader(AttendanceShowInfoActivity.this);
                                        m.execute(ConstUtils.PATH_SD + "/kq/" + month + ".xls");
                                        break;
                        }
                }
        }

        @Override
        protected void onPause() {
                super.onPause();
        }

        @Override
        protected void onDestroy() {
                m.cancel(true);
                dataList = null;
                chartTableView = null;
                LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
                super.onDestroy();
        }
}
